<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot>
          <el-button type="primary" size="small" @click="openDialog(1, 0)">
            添加权限
          </el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-table :data="list" row-key="id" border>
        <el-table-column prop="name" align="center" label="名称" />
        <el-table-column prop="code" align="center" label="标识" />
        <el-table-column prop="description" align="center" label="描述" />
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.type * 1 === 1"
              type="text"
              @click="openDialog(2, scope.row.id)"
            >
              添加
            </el-button>
            <el-button type="text" @click="editRow(scope.row.id)">
              编辑
            </el-button>
            <el-button type="text" @click="delRow(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- //!新增修改权限组件 -->
      <addPerm ref="addPerm" @updateList="initData" />
    </div>
  </div>
</template>

<script>
import addPerm from '@/views/permissions/components/addPerm'
import { tranListToTreeData } from '@/utils/index'
import { delPermissionApi, getPermissionListApi } from '@/api/permission'
export default {
  name: 'Permission',
  components: { addPerm },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      const res = await getPermissionListApi()
      this.list = tranListToTreeData(res, '0')
    },
    openDialog(type, pid) {
      // type就是类型  pid就是父级ID
      this.$refs.addPerm.dialogShow = true
      this.$refs.addPerm.formData.type = type
      this.$refs.addPerm.formData.pid = pid
    },
    editRow(id) {
      console.log(id)
      this.$refs.addPerm.getRowInfo(id)
    },
    async delRow(id) {
      console.log(id)
      try {
        await this.$confirm('确定退出吗')
      } catch (e) {
        return console.log(e)
      }
      await delPermissionApi(id)
      this.$message.success('删除成功')
      this.initData()
    }
  }
}
</script>

<style></style>
